package main

import (
	"fmt"
	"os"
	codecs "sanguo/codec/cs"
	"sanguo/node/client/dispatcher"
	"sanguo/node/client/login"
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage selectRole userID addr\n")
		return
	}

	userID := os.Args[1]
	addr := os.Args[2]

	login.Login(userID, addr, func(session kendynet.StreamSession, d *dispatcher.Dispatcher, msg *codecs.Message, err error) {
		if nil != err {
			fmt.Println(err)
		} else {
			gameLoginToC := msg.GetData().(*cs_msg.GameLoginToC)
			fmt.Println("RoleList", gameLoginToC)
			if gameLoginToC.GetCode() == cs_msg.EnumType_OK {
				var roleIDMap map[int]uint64 = make(map[int]uint64)

				req := &cs_msg.RoleListToS{}
				session.Send(codecs.NewMessage(uint16(0), req))

				d.Register(&cs_msg.RoleListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleListToC)
					fmt.Println(resp.GetErrcode())
					for k, v := range resp.RoleList {
						fmt.Println(k, v)
						roleIDMap[k] = v.GetID()
						fmt.Println(k, roleIDMap[k])
					}
					d.Register(&cs_msg.EnterInstanceZoneToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.EnterInstanceZoneToC)
						fmt.Println("EnterInstanceZoneToC", resp)
						if resp.GetErrcode() == cs_msg.EnumType_OK {
							for _, v := range resp.GetInstanceZoneAward().Awards {
								fmt.Println("---------Award:", v)
							}
							End1(session)
						}

					})
					d.Register(&cs_msg.EnterCampaignToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.EnterCampaignToC)
						fmt.Println("EnterCampaignToC", resp)
						if resp.GetErrcode() == cs_msg.EnumType_OK {
							Enter2(session, resp)
						}

					})

					d.Register(&cs_msg.EndInstanceZoneToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.EndInstanceZoneToC)
						fmt.Println("EndInstanceZoneToC", resp)
						if resp.GetErrcode() == cs_msg.EnumType_OK {
							//End2(session)
							Enter1(session)
						}
					})
					d.Register(&cs_msg.LeaveMapToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.LeaveMapToC)
						fmt.Println("LeaveMapToC", resp)
						if resp.GetErrcode() == cs_msg.EnumType_OK {
							Enter1(session)
						}
					})

					d.Register(&cs_msg.RoleAttrToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleAttrToC)
						fmt.Println("RoleAttrToC ", resp)

					})

					d.Register(&cs_msg.RoleBattleAttrToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleBattleAttrToC)
						fmt.Println("RoleBattleAttrToC ", resp)

					})

					d.Register(&cs_msg.RoleItemToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleItemToC)
						fmt.Println("RoleItemToC ", resp)

					})

					d.Register(&cs_msg.RoleWeaponToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleWeaponToC)
						fmt.Println("RoleWeaponToC ", resp)

					})

					d.Register(&cs_msg.RoleMissionToC{}, false, func(args []interface{}) {
						resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleMissionToC)
						fmt.Println("RoleMissionToC", resp)

					})

					if resp.GetErrcode() == cs_msg.EnumType_OK {
						req2 := &cs_msg.SelectRoleToS{
							ID: proto.Uint64(roleIDMap[0]),
						}
						session.Send(codecs.NewMessage(uint16(0), req2))
						d.Register(&cs_msg.SelectRoleToC{}, false, func(args []interface{}) {
							resp2 := args[1].(*codecs.Message).GetData().(*cs_msg.SelectRoleToC)
							fmt.Println("read selectrole", resp2)
							if resp2.GetErrcode() == cs_msg.EnumType_OK {
								req7 := &cs_msg.EnterMapToS{}
								session.Send(codecs.NewMessage(uint16(0), req7))
							}

							d.Register(&cs_msg.EnterMapToC{}, true, func(args []interface{}) {
								resp := args[1].(*codecs.Message).GetData().(*cs_msg.EnterMapToC)
								fmt.Println("EnterMapToC", resp)
								if resp.GetErrcode() == cs_msg.EnumType_OK {
									req2 := &cs_msg.StartAoiToS{}
									session.Send(codecs.NewMessage(uint16(0), req2))
									Enter1(session)

								}
							})

						})
					}

				})

			}
		}
	})

	sigStop := make(chan bool)
	_, _ = <-sigStop
}

func Enter1(session kendynet.StreamSession) {
	go func() {
		fmt.Printf("编号:")
		var id int
		fmt.Scan(&id)
		//		fmt.Printf("倍率:")
		//		var p int
		//		fmt.Scan(&p)
		//		fmt.Printf("BattleGroundID:")
		//		var d int
		//		fmt.Scan(&d)

		req := &cs_msg.EnterCampaignToS{
			Index:          proto.Int32(int32(id)),
			ExplorePower:   proto.Int32(int32(3)),
			BattleGroundID: proto.Int32(int32(30101)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()

}

func Enter2(session kendynet.StreamSession, resp *cs_msg.EnterCampaignToC) {
	go func() {
		fmt.Println("--------------------")
		req := &cs_msg.EnterInstanceZoneToS{
			InstanceZoneID: proto.Int32(resp.GetBattleLevelID()),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()

}

func End1(session kendynet.StreamSession) {
	go func() {

		req := &cs_msg.EndInstanceZoneToS{
			IsFinished: proto.Bool(true),
		}
		session.Send(codecs.NewMessage(uint16(0), req))

	}()

}

func End2(session kendynet.StreamSession) {
	go func() {

		req := &cs_msg.LeaveMapToS{}
		session.Send(codecs.NewMessage(uint16(0), req))

	}()

}
